package com.example.CarFee;

import java.util.HashMap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.trainfee.R;

public class LoggingActivity extends Activity {
	private Button logging_btn, register_btn;
	private EditText account, password;
	private ProgressDialog bar;
	private boolean isLogin = false;
	private String[] info;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activtity_logging);
		logging_btn = (Button) findViewById(R.id.logging_btn);
		register_btn = (Button) findViewById(R.id.register_btn);
		account = (EditText) findViewById(R.id.account);
		password = (EditText) findViewById(R.id.psw);
		bar = new ProgressDialog(this);
		bar.setTitle("登录中");
		bar.setMessage("请稍后...");
		logging_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Thread thread = new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						isLogin = Service.denglu(account.getText().toString(), password
								.getText().toString());
						if(isLogin)
						{
							Info.account = account.getText().toString();
							Info.psw = password.getText().toString();
							Service.currentAccount = Info.account;
							Service.currentPassword = Info.psw;
							info = Service.checkPersonalInfo();
							handler.sendEmptyMessage(0x120);
						}else{
							handler.sendEmptyMessage(0x121);
							
						}
					}
				});
				thread.start();
				bar.show();
				// Intent intent = new Intent(LoggingActivity.this,
				// MainActivity.class);
				// startActivity(intent);
			}
		});
		register_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(LoggingActivity.this,
						RegisterActivity.class);
				startActivity(intent);
			}
		});
	}
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0x120) {
				bar.cancel();
				if (isLogin) {
					
					Toast.makeText(LoggingActivity.this, "登陸成功",
							Toast.LENGTH_SHORT).show();
					if (!info[0].equals("")) {
						Info.name = info[0];
						Info.sex = info[1];
						Info.place = info[2];
						Info.book = info[3];
						Info.station = info[4];
						Info.date = info[5];
						Info.time = info[6];
						if (info.length > 7) {
							Info.phone = info[7];
						}
					}
					Intent intent = new Intent(LoggingActivity.this,
							MainActivity.class);
					startActivity(intent);
					finish();
				} else {
					Toast.makeText(LoggingActivity.this, "账号或密码错误",
							Toast.LENGTH_SHORT).show();

				}
			}
			if (msg.what == 0x121) {
				bar.cancel();
				Toast.makeText(LoggingActivity.this, "账号或密码错误",
						Toast.LENGTH_SHORT).show();
			}
		}
	};
}
